package com.bw.controller;

import com.bw.bean.Spu;
import com.bw.service.Spuservice;
import com.bw.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Spucontroller {

    @Resource
    private Spuservice spuservice;

    @RequestMapping("list.do")
    public String list(@RequestParam(value = "cpage",defaultValue = "1")Integer cpage,
                       String mohu,
                       String biaoti,
                       String xiala,String xulie,
                       Model model){
        Map map = new HashMap();
        map.put("mohu",mohu);
        map.put("xiala",xiala);
        map.put("xulie",xulie);
        PageHelper.startPage(cpage,3);
        List list = spuservice.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("map",map);
        model.addAttribute("pageInfo",pageInfo);
        return "list";
    }
    @RequestMapping("toadd")
    public String toadd(){
        return "add";
    }
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Spu spu, MultipartFile photo, HttpSession session){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            spu.setPic(upload);
        }
        int add = spuservice.add(spu);
        return add;
    }

//    listgoods
    @RequestMapping("listgoods.do")
    @ResponseBody
    public Object listgoods(Integer pid){
        List listgoods = spuservice.listgoods(pid);
        return listgoods;
    }
    @RequestMapping("listxl.do")
    @ResponseBody
    public Object listxl(Integer bid){
        List listxl = spuservice.listxl(bid);
        return listxl;
    }


    @RequestMapping("toupd.do")
    public String toupd(){
        return "upd";
    }
    @RequestMapping("upd.do")
    @ResponseBody
    public Object upd(Spu spu, MultipartFile photo, HttpSession session){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            spu.setPic(upload);
        }
        int upd = spuservice.upd(spu);
        return upd;
    }


//    getByid
@RequestMapping("getByid.do")
@ResponseBody
public Object getByid(Integer sid){
    Map byid = spuservice.getByid(sid);
    return byid;
}

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer sid){
        int del = spuservice.del(sid);
        return del;
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String  sid){
        int dels = spuservice.dels(sid);
        return dels;
    }

}


